# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=uriparser
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.4
pkgrel=1
pkgdesc="RFC 3986 URI parsing and processing libary (mingw-w64)"
arch=('any')
url="https://uriparser.sourceforge.io/"
license=('New BSD License')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gtest>=1.8.1")
source=("https://downloads.sourceforge.net/project/${_realname}/Sources/${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "${_realname}-0.9.2.patch")
sha256sums=('b7cdabe5611408fc2c3a10f8beecb881a0c7e93ff669c578cd9e3e6d64b8f87b'
            '8823758d69f80ab818217f0be015c08389803621b8983eccc5456e35658b9922')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-0.9.2.patch"
}

build() {
  # Shared
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

  make

  # Static
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make check || true
}

package() {
  # Shared
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR="${pkgdir}" install

  # Static
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make DESTDIR="${pkgdir}" install
}
